package com.sbl.bajw.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Properties;

import org.junit.Test;

/**
 * 读取Properties的工具类
 * @author Administrator
 *
 */
public class PropertiesUnit {
	
	static String propertiesPath;
	
	public static String getPropertiesPath() {
		return propertiesPath;
	}
	
	public static void setPropertiesPath(String propertiesPath) {
		PropertiesUnit.propertiesPath = propertiesPath;
	}
	
	/**
	 * 根据字段名读取属性
	 * @param name
	 * @return
	 */
	public static String getValue(String name)
	{
		Properties p=null;
		String value=null;
		try {
			p = loadProperties();
			value= p.getProperty(name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return value;
	}
	/**
	 * 获取Properties对象
	 * @return
	 * @throws IOException
	 * @throws URISyntaxException 
	 */
	public static Properties loadProperties() throws IOException, URISyntaxException
	{
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("output.properties");
		Properties p = new Properties();
		p.load(in);
		return p;
	}
	
	public static String GetJarDir(){
		
		String path=null;
		
		path = PropertiesUnit.class.getProtectionDomain().getCodeSource().getLocation().getPath();//PropertiesUnit.class.getResource("/").toURI().getPath();
		File jarDir=new File(path).getParentFile();
		return jarDir.getPath();
		
	}
	
	/**
	 * 获取到项目根目录下的路径，项目是evlocean的话，返回的就是 *************\evlocean\
	 * @return
	 */
	public static String getBasePath() {
		String path;
		try {
			path = new File(PropertiesUnit.class.getResource("/").toURI().getPath()).getParentFile().getParent();
			return new File(path).toURI().toString().replaceFirst("file:/", "");
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@Test
	public void test(){
		String value = getValue("name");
		System.out.println(value);
	}
}